﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using jQuery.WebControls;


namespace jQuery.WebControls.UI
{
    /// <summary>
    /// Control formado por un TextBox que incorpora un DatePicker de jQuery UI para seleccionar una fecha en concreto
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:Slider runat=server></{0}:Slider>")]
    public class Slider : HiddenField
    {

        private ClientScriptProxy scriptManager;
        private jQueryScriptManager jQueryMngr;

        #region "PROPIEDADES"
        private jQueryScriptManager jQueryManager
        {
            get
            {
                if (jQueryMngr == null)
                    jQueryMngr = Common.Instance.Get_jQueryScriptManager(Page);                
                return jQueryMngr;
            }
        }

        /// <summary>
        /// Gets or sets the range minimum value
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(0)]
        [Localizable(true)]
        [Description("Gets or sets the range minimum value")]
        public Decimal Minimum
        {
            get
            {
                if (ViewState["min"] == null)
                {
                    return 0;
                }
                else
                {
                    return Decimal.Parse(ViewState["min"].ToString());
                }
            }
            set
            {
                ViewState["min"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the range maximum value
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(0)]
        [Localizable(true)]
        [Description("Gets or sets the range maximum value")]
        public Decimal Maximum
        {
            get
            {
                if (ViewState["max"] == null)
                {
                    return 0;
                }
                else
                {
                    return Decimal.Parse(ViewState["max"].ToString());
                }
            }
            set
            {
                ViewState["max"] = value;
            }
        }

        /// <summary>
        /// Gets or sets step to slide
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(1)]
        [Localizable(true)]
        [Description("Gets or sets step to slide")]
        public Decimal Step
        {
            get
            {
                if (ViewState["stp"] == null)
                {
                    return 1;
                }
                else
                {
                    return Decimal.Parse(ViewState["stp"].ToString());
                }
            }
            set
            {
                ViewState["stp"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the slider type
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(SliderType.Simple)]
        [Localizable(true)]
        [Description("Gets or sets the slider type")]
        public SliderType SliderType
        {
            get
            {
                if (ViewState["slt"] == null)
                {
                    return SliderType.Simple;
                }
                else
                {
                    return (SliderType)Enum.Parse(typeof(SliderType), ViewState["slt"].ToString());
                }
            }
            set
            {
                ViewState["slt"] = value;
            }
        }

        /// <summary>
        /// Sets the javascript function that it will be called when the slider value change. This function will receive an argument of jquery.ui type
        /// <code language="javascript">
        /// function SliderValueChanged(ui){
        /// alert(ui.value); // when it's a simple slider
        /// alert(ui.values); // when it's a range
        /// }
        /// </code>
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]        
        [Localizable(true)]
        [Description("Sets the javascript function that it will be called when the slider value change. This function will receive an argument of jquery.ui type")]
        public string OnClientChange
        {            
            set
            {
                ViewState["cc"] = value;
            }
        }

        /// <summary>
        /// Gets or sets if the a label with the value selected must be shown
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(false)]
        [Localizable(true)]
        [Description("Gets or sets if the a label with the value selected must be shown")]
        public bool ShowLabel
        {
            get
            {
                if (ViewState["sl"] == null)
                {
                    return false;
                }
                else
                {
                    return Boolean.Parse(ViewState["sl"].ToString());
                }
            }
            set
            {
                ViewState["sl"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the label text. 
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("Value = ")]
        [Localizable(true)]
        [Description("Gets or sets the label text")]
        public string LabelText
        {
            get
            {
                if (ViewState["lt"] == null)
                {
                    return "";
                }
                else
                {
                    return ViewState["lt"].ToString();
                }
            }
            set
            {
                ViewState["lt"] = value;
            }
        }

        public Decimal MinimumValue
        {
            get
            {
                if (Value.IndexOf(",") > -1)
                {
                    return Decimal.Parse(Value.Split(',')[0]);
                }
                else
                {
                    Decimal aux = 0;
                    Decimal.TryParse(Value, out aux);
                    return aux;
                }
            }
        }

        public Decimal MaximumValue
        {
            get
            {
                if (Value.IndexOf(",") > -1)
                {
                    return Decimal.Parse(Value.Split(',')[1]);
                }
                else
                {
                    Decimal aux = 0;
                    Decimal.TryParse(Value, out aux);                    
                    return aux;
                }
            }
        }
        
        #endregion

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            string script = "$(document).ready(function(){";
            script += "$('#slider-" + ClientID + "').slider({";
            script += String.Format("range: {0},", SliderType == SliderType.Simple ? "false" : "true");
            script += String.Format("min: {0},", Minimum);
            script += String.Format("max: {0},", Maximum);
            script += String.Format("step: {0},", Step);
            if (SliderType == SliderType.Simple)
            {
                script += String.Format("value: {0},", Value);
                script += "slide: function(event, ui){";
                script += String.Format(" $(\"#{0}\").val(ui.value);", ClientID);
                if (ShowLabel)
                {
                    script += String.Format(" $(\"#lb{0}\").html('{1} ' + ui.value);", ClientID, LabelText);
                }
                script += "}";  
            }
            else
            {
                script += String.Format("values: [{0},{1}],", MinimumValue, MaximumValue);
                script += "slide: function(event, ui){";
                script += String.Format(" $(\"#{0}\").val(ui.values[0] + ',' + ui.values[1]);", ClientID);
                if (ShowLabel)
                {
                    script += String.Format(" $(\"#lb{0}\").html('{1} ' + ui.values[0] + ' - ' + ui.values[1]);", ClientID, LabelText);
                }
                script += "}";  
            }
                                             
            script += "});});";
            ClientScriptProxy.Current.RegisterClientScriptBlock(this, this.GetType(), "sld1", script, true);
        }

        
        public override void RenderControl(HtmlTextWriter writer)
        {
            base.RenderControl(writer);
            if (ShowLabel)
            {
                if (SliderType == SliderType.Simple)
                {
                    writer.WriteLine(String.Format("<div><label id=\"lb{0}\">{1} {2}</label></div>", ClientID, LabelText, Value));
                }
                else
                {
                    writer.WriteLine(String.Format("<div><label id=\"lb{0}\">{1} {2} - {3}</label></div>", ClientID, LabelText, MinimumValue, MaximumValue));
                }
            }
            writer.WriteLine(String.Format("<div id=\"slider-{0}\"></div>", ClientID));
        }
       
    }
}
